home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / asm32.zip / E32.ZIP / FINDCHR.ASM < prev    next >
Assembly Source File  |  1995-01-03  |  3KB  |  140 lines

  1. ; FINDCHR.ASM for E32 - Copyright (C) 1994 Douglas Herr
  2. ;  all rights reserved
  3.  
  4. ; call with FS = file selector
  5.  
  6. include    model.inc
  7.  
  8. public    find_start, find_CR
  9. public    find_next, find_eol
  10. public    find_previous
  11.  
  12. CR    equ    0Dh
  13. LF    equ    0Ah
  14.  
  15. include    dataseg.inc
  16.  
  17. extrn    cursor:dword
  18. extrn    filesiz:dword
  19.  
  20. @curseg    ends
  21.  
  22. include    codeseg.inc
  23.  
  24. ;
  25. ; This subroutine finds the beginning of the previous line
  26. ;
  27. find_previous    proc
  28.     push    fs
  29.     pop    es
  30.     push    cursor
  31.     call    find_CR        ; find start of this line
  32.     mov    cursor,esi
  33.     call    find_start    ; find the start of this line
  34.     pop    cursor
  35.     ret
  36. find_previous    endp
  37.  
  38. ;
  39. ; This searches for the previous CR, starting at ESI
  40. ;
  41. find_CR    proc    near
  42.     push    ecx
  43.     push    fs
  44.     pop    es
  45.     mov    al,cr
  46.     mov    edi,esi
  47.     mov    ecx,esi
  48.     jecxz    short at_beginning
  49.     dec    edi
  50.     std
  51.     repne    scasb
  52.     cld
  53.     inc    edi
  54.     mov    esi,edi
  55. at_beginning:
  56.     pop    ecx
  57.     ret
  58. find_CR    endp
  59.  
  60. ;-------------------------------------------------------------------------;
  61. ;   This subroutine computes the location of the start of current line.   ;
  62. ;   Returns ESI pointing to the first character of the current line.      ;
  63. ;-------------------------------------------------------------------------;
  64. find_start    proc    near
  65.     mov    esi,cursor
  66.     test    esi,esi        ; at start of file?
  67.     jz    short at_start
  68.     call    find_CR
  69.     call    skipCRLF
  70. at_start:
  71.     ret
  72. find_start    endp
  73.  
  74. ;-------------------------------------------------------------------------;
  75. ;   This finds the offset of the start of the next line.  The search is   ;
  76. ;   started at location ES:SI.  On return CF = 1 if no CR was found.      ;
  77. ;-------------------------------------------------------------------------;
  78. find_next    proc
  79.     push    ecx
  80.     call    find_eol
  81.     jc    short at_next
  82.     call    skipCRLF
  83.     clc
  84. at_next:pop    ecx
  85.     ret
  86. find_next    endp
  87.  
  88. ;----------------------------------------------------;
  89. ;   This searches for the next CR in the file.       ;
  90. ;   The search starts at the offset in register SI.  ;
  91. ;----------------------------------------------------;
  92. find_eol    proc near
  93.     mov    al,CR        ; look for CR
  94.     push    fs
  95.     pop    es
  96.     mov    ecx,filesiz    ; last letter in file
  97.     sub    ecx,esi        ; count for the search
  98.     mov    edi,esi
  99.     jecxz    short at_end
  100.     repne    scasb
  101.     mov    esi,edi
  102.     jecxz    short at_end
  103.     dec    esi
  104.     clc
  105.     ret
  106. at_end:    stc
  107.     ret
  108. find_eol    endp
  109.  
  110.  
  111. ; skipCRLF
  112. ;
  113. ; This subroutine skips past the CR and LF at ESI
  114. ; ESI returns new offset.
  115. ;
  116. ; call with: ESI = position in file
  117. ;            FS = file selector
  118. ; uses: ES, ESI
  119.  
  120. skipCRLF:
  121.     push    es
  122.     push    fs
  123.     pop    es
  124.     cmp    esi,filesiz        ; at last character in file?
  125.     jae    short no_skip        ; if so, no skip
  126.     cmp    byte ptr es:[esi],CR    ; at CR?
  127.     jne    short no_skip
  128.     inc    esi            ; look at next char
  129.     cmp    esi,filesiz        ; at last char in file?
  130.     jae    short no_skip
  131.     cmp    byte ptr es:[esi],LF    ; at LF?
  132.     jne    short no_skip
  133.     inc    esi
  134. no_skip:
  135.     pop    es
  136.     ret
  137.  
  138. @curseg    ends
  139.     end
  140.